#include "qartiklikorekcija.h"
#include <QSqlQuery>
#include <QMessageBox>
#include <QDesktopWidget>
#include "chelperclass.h "


QArtikliKorekcija::QArtikliKorekcija(QWidget *parent)
	: QMyBaseFormWidget(parent)
	,m_id(0)
{
	ui.setupUi(this);
	QDesktopWidget desk;
	QRect deskRect = desk.screenGeometry();
	ui.layoutWidget->setFixedWidth(deskRect.width() - 300);
//	ui.layoutWidget->setFixedHeight(deskRect.height()-200);
	ui.lineEdit_2->setFocus();
	ui.lineEdit_2->setStyleSheet("background-color: yellow");
	ui.lineEdit_2->installEventFilter(this);
	ui.lineEdit_3->installEventFilter(this);
	ui.lineEdit_4->installEventFilter(this);
	ui.lineEdit_5->installEventFilter(this);
	ui.lineEdit_6->installEventFilter(this);
	ui.lineEdit_7->installEventFilter(this);
	ui.lineEdit_8->installEventFilter(this);
	ui.lineEdit_9->installEventFilter(this);
	ui.lineEdit_10->installEventFilter(this);
	ui.lineEdit_11->installEventFilter(this);
	connect(ui.lineEdit_7, SIGNAL(editingFinished()), this, SLOT(lineEdit7EditingFinished()));
	connect(ui.lineEdit_8, SIGNAL(editingFinished()), this, SLOT(lineEdit8EditingFinished()));
	connect(ui.lineEdit_9, SIGNAL(editingFinished()), this, SLOT(lineEdit9EditingFinished()));
	connect(ui.lineEdit_10, SIGNAL(editingFinished()), this, SLOT(lineEdit10EditingFinished()));
	connect(ui.lineEdit, SIGNAL(editingFinished()), this, SLOT(lineEditEditingFinished()));

}

void QArtikliKorekcija::pressF9()
{
	on_pushButton_clicked();
}


void QArtikliKorekcija::lineEditEditingFinished()
{
	CHelperClass hclass(this);
	if (!hclass.isKomintentExists(ui.lineEdit->text()))
	{
		emit scallKomintenti((QWidget*)this, ui.lineEdit->text());
	}
	else
	{
		ui.lineEdit_4->setFocus();
	}
}

void QArtikliKorekcija::lineEdit7EditingFinished()
{
	QLocale loc;
	float ddv = loc.toFloat(ui.lineEdit_11->text());
	float cena1 = loc.toFloat(ui.lineEdit_7->text());
	ui.lineEdit_8->setText(loc.toString(cena1 * (1 + ddv / 100.f), 'f', 2));
	ui.lineEdit_7->setText(loc.toString(cena1, 'f', 2));
}

void QArtikliKorekcija::lineEdit8EditingFinished()
{
	QLocale loc;
	float ddv = loc.toFloat(ui.lineEdit_11->text());
	float cena1 = loc.toFloat(ui.lineEdit_8->text());
	ui.lineEdit_7->setText(loc.toString(cena1 / (1 + ddv / 100.f), 'f', 2));
	ui.lineEdit_8->setText(loc.toString(cena1, 'f', 2));
}

void QArtikliKorekcija::lineEdit9EditingFinished()
{
	QLocale loc;
	float ddv = loc.toFloat(ui.lineEdit_11->text());
	float cena1 = loc.toFloat(ui.lineEdit_9->text());
	ui.lineEdit_10->setText(loc.toString(cena1 * (1 + ddv / 100.f), 'f', 2));
	ui.lineEdit_9->setText(loc.toString(cena1, 'f', 2));
}

void QArtikliKorekcija::lineEdit10EditingFinished()
{
	QLocale loc;
	float ddv = loc.toFloat(ui.lineEdit_11->text());
	float cena1 = loc.toFloat(ui.lineEdit_10->text());
	ui.lineEdit_9->setText(loc.toString(cena1 / (1 + ddv / 100.f), 'f', 2));
	ui.lineEdit_10->setText(loc.toString(cena1, 'f', 2));
}

QArtikliKorekcija::~QArtikliKorekcija()
{

}
void QArtikliKorekcija::showData(const QString& id)
{
	QSqlQuery query;
	query.prepare("select * from artikli where id = :a");
	query.bindValue(":a", id);
	query.exec();
	query.next();

	m_id = query.value(0).toInt();
	ui.lineEdit_2->setText(query.value(1).toString());
	ui.lineEdit_3->setText(query.value(2).toString());
	ui.lineEdit_4->setText(query.value(3).toString());
	ui.lineEdit_5->setText(query.value(4).toString());	
	ui.lineEdit_6->setText(query.value(5).toString());
	ui.lineEdit_7->setText(query.value(6).toString());
	ui.lineEdit_8->setText(query.value(7).toString());
	ui.lineEdit_9->setText(query.value(8).toString());
	ui.lineEdit_10->setText(query.value(9).toString());
    ui.lineEdit_11->setText(query.value(10).toString());
	ui.lineEdit->setText(query.value(11).toString());
    QString textTemp = query.value(12).toString();
    if (textTemp.contains("1"))
    {
        ui.checkBox->setDisabled(false);
        ui.checkBox->setChecked(true);
    }
    else
    {
        ui.checkBox->setDisabled(false);
        ui.checkBox->setChecked(false);
    }
}


void QArtikliKorekcija::on_pushButton_clicked()
{
	
	QLocale loc;
	
	QMessageBox msgBox;
	msgBox.setText(trUtf8("Документот ке биде изменет."));
	msgBox.setInformativeText(trUtf8("Дали сакате да ги сочувате промените?"));
	msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
	msgBox.setDefaultButton(QMessageBox::Ok);	int ret = msgBox.exec();
	if (ret == QMessageBox::Ok )
	{
		QSqlQuery query;
		query.prepare("update artikli set sifra=:sifra, "
			"artikal =:artikal, "
			"edm =:edm, "
			"ref =:ref, "
			"kataloski_broj=:kb ,"
			"art_nabavna_cena=:anc, "
			"art_nabavna_cena_valuta=:ancv, "
			"art_prodazna_cena=:apc, "
			"art_prodazna_cena_valuta=:apcv, "
            "ddv=:iddv,"
			"proizvoditel=:aproiz,"
            "kategorija = :akateg "
            "where id=:id");
		query.bindValue(":id", m_id);
		query.bindValue(":sifra", ui.lineEdit_2->text());
		query.bindValue(":artikal", ui.lineEdit_3->text());
		query.bindValue(":edm", ui.lineEdit_4->text());
		query.bindValue(":ref", ui.lineEdit_5->text());
		query.bindValue(":kb", ui.lineEdit_6->text());
		query.bindValue(":anc", loc.toFloat(ui.lineEdit_7->text()));
		query.bindValue(":ancv", loc.toFloat(ui.lineEdit_8->text()));
		query.bindValue(":apc", loc.toFloat(ui.lineEdit_9->text()));
		query.bindValue(":apcv", loc.toFloat(ui.lineEdit_10->text()));
        query.bindValue(":iddv", loc.toFloat(ui.lineEdit_11->text()));
		query.bindValue(":aproiz", ui.lineEdit->text());
        query.bindValue(":akateg", ui.checkBox->isChecked() ? "1" : "0");
		if(query.exec())
		{
			QMessageBox msgBox;
			msgBox.setText(trUtf8("Трансакцијата е успешна"));
			msgBox.setStandardButtons(QMessageBox::Ok);
			msgBox.setDefaultButton(QMessageBox::Ok);
			msgBox.exec();
			emit succesfulEntryData();
		}
		else
		{
			QMessageBox msgBox;
			msgBox.setText(trUtf8("Трансакцијата ne е успешна"));
			msgBox.setStandardButtons(QMessageBox::Ok);
			msgBox.setDefaultButton(QMessageBox::Ok);
			msgBox.exec();
		}
	}
}

void QArtikliKorekcija::pressEscape()
{
	emit closeW();
}

void QArtikliKorekcija::pressReturn()
{
	if (ui.lineEdit_2->hasFocus())
	{
		ui.lineEdit_3->setFocus();
	}
	else if (ui.lineEdit_3->hasFocus())
	{
		ui.lineEdit->setFocus();
	}
	else if (ui.lineEdit->hasFocus())
	{
		ui.lineEdit_4->setFocus();
	}
	else if (ui.lineEdit_4->hasFocus())
	{
		ui.lineEdit_5->setFocus();
	}
	else if (ui.lineEdit_5->hasFocus())
	{
		ui.lineEdit_6->setFocus();
	}
	else if (ui.lineEdit_6->hasFocus())
	{
		ui.lineEdit_11->setFocus();
	}
	else if (ui.lineEdit_7->hasFocus())
	{
		ui.lineEdit_8->setFocus();
	}
	else if (ui.lineEdit_8->hasFocus())
	{
		ui.lineEdit_9->setFocus();
	}
	else if (ui.lineEdit_9->hasFocus())
	{
		ui.lineEdit_10->setFocus();
	}
	else if (ui.lineEdit_10->hasFocus())
	{
		on_pushButton_clicked();
	}
	else if (ui.lineEdit_11->hasFocus())
	{
		ui.lineEdit_7->setFocus();
	}
}

bool QArtikliKorekcija::eventFilter(QObject *object, QEvent *event)
{
    if (event->type() == QEvent::FocusIn)
    {
        if (object == ui.lineEdit_2)
        {
            ui.lineEdit_2->setStyleSheet("background-color: yellow");
        }
        if (object == ui.lineEdit_3)
        {
            ui.lineEdit_3->setStyleSheet("background-color: yellow");
        }
        if (object == ui.lineEdit_4)
        {
            ui.lineEdit_4->setStyleSheet("background-color: yellow");
        }
        if (object == ui.lineEdit_5)
        {
            ui.lineEdit_5->setStyleSheet("background-color: yellow");
        }
        if (object == ui.lineEdit_6)
        {
            ui.lineEdit_6->setStyleSheet("background-color: yellow");
        }
        if (object == ui.lineEdit_7)
        {
            ui.lineEdit_7->setStyleSheet("background-color: yellow");
        }
        if (object == ui.lineEdit_8)
        {
            ui.lineEdit_8->setStyleSheet("background-color: yellow");
        }
        if (object == ui.lineEdit_9)
        {
            ui.lineEdit_9->setStyleSheet("background-color: yellow");
        }
        if (object == ui.lineEdit_10)
        {
            ui.lineEdit_10->setStyleSheet("background-color: yellow");
        }
        if (object == ui.lineEdit_11)
        {
            ui.lineEdit_11->setStyleSheet("background-color: yellow");
        }
		if (object == ui.lineEdit)
		{
			ui.lineEdit->setStyleSheet("background-color: yellow");
		}
    }
    if (event->type() == QEvent::FocusOut)
    {
        if (object == ui.lineEdit_2)
        {
            ui.lineEdit_2->setStyleSheet("background-color: none");
        }
        if (object == ui.lineEdit_3)
        {
            ui.lineEdit_3->setStyleSheet("background-color: none");
        }
        if (object == ui.lineEdit_4)
        {
            ui.lineEdit_4->setStyleSheet("background-color: none");
        }
        if (object == ui.lineEdit_5)
        {
            ui.lineEdit_5->setStyleSheet("background-color: none");
        }
        if (object == ui.lineEdit_6)
        {
            ui.lineEdit_6->setStyleSheet("background-color: none");
        }
        if (object == ui.lineEdit_7)
        {
            ui.lineEdit_7->setStyleSheet("background-color: none");
        }
        if (object == ui.lineEdit_8)
        {
            ui.lineEdit_8->setStyleSheet("background-color: none");
        }
        if (object == ui.lineEdit_9)
        {
            ui.lineEdit_9->setStyleSheet("background-color: none");
        }
        if (object == ui.lineEdit_10)
        {
            ui.lineEdit_10->setStyleSheet("background-color: none");
        }
        if (object == ui.lineEdit_11)
        {
            ui.lineEdit_11->setStyleSheet("background-color: none");
        }
		if (object == ui.lineEdit)
		{
			ui.lineEdit_11->setStyleSheet("background-color: none");
		}
    }
    return false;
}

void QArtikliKorekcija::setKomintent(QString text, QString textName)
{
	ui.lineEdit->setText(textName);
}


void QArtikliKorekcija::setFocusLine()
{
	ui.lineEdit_4->setFocus();
}